package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;
public class KargoBiletMapper implements IMapper {

	@Override
	public Object get(int ID) {
		KargoBilet kargobilet = new KargoBilet();
		ResultSet rs = DataConnector.getInstance().query("select * from kargobilet where biletid = " + ID + "");
		int int4 = 0;
		int int5 = 0;
		if (rs != null){
            
            try {
            		rs.next();
                    int4 = Integer.parseInt(rs.getString(4));
                    int5 = Integer.parseInt(rs.getString(5));

            		
            		kargobilet.setBiletID(Integer.parseInt(rs.getString(1)));
            		kargobilet.setTarih(new GregorianCalendar(Integer.parseInt(rs.getString(2).split("-")[0]), 
							Integer.parseInt(rs.getString(2).split("-")[1]), 
							Integer.parseInt(rs.getString(2).split("-")[2])));
            		kargobilet.setHacim(Float.valueOf(rs.getString(3)));
            		
   
                             return kargobilet;
                   
            } catch (NumberFormatException e) {
                    System.out.println("KargoBiletMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("KargoBiletMapper: sql problem");
                    e.printStackTrace();
            }
		}
		else
            System.out.println("no kargobilet with given ID");
		
		MusteriMapper mm = new MusteriMapper();
        Musteri m = (Musteri) mm.get(int4);
        KargoUcusMapper yum = new KargoUcusMapper();
        KargoUcus yu = (KargoUcus) yum.get(int5);
		
		kargobilet.setMusteri(m);
		kargobilet.setKargoUcus(yu);
		return null;
	}

	@Override
	public int put(Object object) {
		KargoBilet kargobilet=(KargoBilet) object;
		String dt = "";
		GregorianCalendar dta = kargobilet.getTarih();
		dt += String.valueOf(dta.get(GregorianCalendar.YEAR)) + "-";
		dt += String.valueOf(dta.get(GregorianCalendar.MONTH)) + '-';
		dt += String.valueOf(dta.get(GregorianCalendar.DAY_OF_MONTH));
		if(!DataConnector.getInstance().update("INSERT INTO kargobilet "+
				"(biletid, tarih, hacim, musteri, kargoucus ) " +
				"values ('"+kargobilet.getBiletID()+"', '"+
				dt+"', '"+
				kargobilet.getHacim()+"', '"+
				kargobilet.getMusteri().getMusteriID()+"', '"+
				kargobilet.getKargoUcus().getUcusID()+			
				"')")){
			System.out.println("error");
		}
        
		return 0;
	}

	@Override
	public List<Object> getAll() {
		List<Object> allKargoBiletList = new ArrayList<Object>();
    	ResultSet rs = DataConnector.getInstance().query("select * from kargobilet;");
    	int numberOfRows=0;
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
            numberOfRows = rsMetaData.getColumnCount()+1;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

    try {
    	if (rs != null){
            while(rs.next()){
                    
                    KargoBilet kargobilet= new KargoBilet();                     	
                    	Musteri m = new Musteri(Integer.parseInt(rs.getString(4)));
                    	KargoUcus ku = new KargoUcus();
                    	ku.setUcusID(Integer.parseInt(rs.getString(5)));
                    	                    	
                    	kargobilet.setBiletID(Integer.parseInt(rs.getString(1)));
                		kargobilet.setTarih(new GregorianCalendar(Integer.parseInt(rs.getString(2).split("-")[0]), 
    							Integer.parseInt(rs.getString(2).split("-")[1]), 
    							Integer.parseInt(rs.getString(2).split("-")[2])));
                		kargobilet.setHacim(Float.valueOf(rs.getString(3)));
                		
                		kargobilet.setMusteri(m);
                		kargobilet.setKargoUcus(ku);
                            allKargoBiletList.add(kargobilet);
              
            }
    	}
    }
    catch(Exception e){
    	e.printStackTrace();
    }
    	for (Object se: allKargoBiletList){
    		KargoBilet kb = (KargoBilet) se;
    		KargoUcusMapper kum = new KargoUcusMapper();
    		MusteriMapper mm = new MusteriMapper();
    		
    		KargoUcus k = (KargoUcus) kum.get(kb.getKargoUcus().getUcusID());
    		Musteri m = (Musteri) mm.get(kb.getMusteri().getMusteriID());
    		
    		kb.setMusteri(m);
    		kb.setKargoUcus(k);
    	}
    	return allKargoBiletList;		
	
	}

}
